﻿Imports Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Public Class GetUserOU
    Public Shared Function GetUserOU(ByVal username As String, ByVal connection As WqlConnectionManager) As String
        Dim strUserOU As String
        Dim userPrincipalName As String
        Dim mail As String
        strUserOU = ""
        userPrincipalName = ""
        Dim splituser = username.Split("\")
        Dim objUser =
                connection.QueryProcessor.ExecuteQuery(
                    "select * from SMS_R_User where SMS_R_User.UniqueUserName = '" & splituser(0) & "\\" & splituser(1) &
                    "'")
        For Each resUser In objUser
            If resUser.Properties("UserOUName").StringArrayValue.length = 0 Then
                strUserOU =
                    resUser.Properties("UserContainerName").StringArrayValue(
                        resUser.Properties("UserContainerName").StringArrayValue.length - 1).ToString()
            Else
                strUserOU =
                    resUser.Properties("UserOUName").StringArrayValue(
                        resUser.Properties("UserOUName").StringArrayValue.length - 1).ToString()
            End If
            userPrincipalName = resUser.Properties("UserPrincipalName").stringvalue.ToString()
            mail = resUser.Properties("Mail").stringvalue.ToString()
        Next
        If strUserOU = Nothing Then strUserOU = "User Details not Discovered in SCCM"
        If userPrincipalName = Nothing Then userPrincipalName = "User Details not Discovered in SCCM"
        Return strUserOU & "|" & userPrincipalName & "|" & mail
    End Function
End Class
